<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: BaseController.php
 * +----------------------------------------------------------------------
 * | Author: lvyinhong
 * +----------------------------------------------------------------------
 * | Description: 还款减免接口
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History:
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/3/4 Author: lvyinhong Modification: 创建
 * +----------------------------------------------------------------------
 */
namespace App\Http\Controllers\Collection;

use App\Models\CollectionApiRequest;
use App\Models\RepayRecord;
use App\Models\RepayReliefRecordModel;
use App\Models\RepaySummary;
use Illuminate\Support\Facades\Log;

class RepayReliefController extends BaseController
{

    /**
     * 添加一次减免记录
     * @return mixed
     * @throws \Throwable
     */
    public function add()
    {
        $data = $this->pubData;
        log::debug('减免数据',['reduce'=>$data]);
        // 查询还款详情
        $list = \DB::table('repay_summaries')
            ->select(['repay_records.normal_date as normal_time', 'repay_summaries.rate as rate', 'repay_summaries.amount as amount', 'repay_record_commons.id as common_id'])
            ->leftJoin('repay_records','repay_summaries.id', '=', 'repay_records.summary_id')
            ->leftJoin('repay_record_commons','repay_summaries.id', '=','repay_record_commons.summary_id')
            ->where('repay_summaries.loan_id',$data['caseCode'])
            ->where('repay_records.period', $data['period'])
            ->where('repay_records.status', '<>', RepayRecord::STATUS_FOUR)
            ->orderBy('repay_summaries.created_at')
            ->first();
        if(!$list) {
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '当前还款信息不存在或已还清';
            return collectionResp($returnData);
        }
        // 查询当前还款减额记录
        RepayReliefRecordModel::clearData();
        $reliefList  = RepayReliefRecordModel::where('loan_id', $data['caseCode'])
            ->where('period', $data['period'])
            ->where('status',1)
            ->orderBy('created_at')
            ->get();
        $reliefAmount = $data['reliefAmount']; // 总减额(本次提交金额)
        foreach ($reliefList as $relief) {
            if($relief->status == RepayReliefRecordModel::STATUS_UNUSED) {
                $reliefAmount+=$relief->relief_amount;
            }
        }
        // 获取罚息利率
        $finesRate = json_decode($list->rate, JSON_UNESCAPED_UNICODE)['fines_rate'];
        // 获取当前公共罚息金额
        //$commonFines = RepaySummary::getFines($list->normal_time, $finesRate, $list->amount,$list->common_id,false)['common_fines_money'];
        $commonFines=RepaySummary::leftJoin('repay_record_commons as rrc','rrc.summary_id','=','repay_summaries.id')
            ->where('repay_summaries.loan_id',$data['caseCode'])->where('rrc.period',$data['period'])->first();
        $commonFines=bcdiv($commonFines->toArray()['fines_money'],100,2);
        if($reliefAmount>$commonFines){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = "减免金额不能大于罚息";
            return collectionResp($returnData);
        }

        $deadline = date('Y-m-d'). ' 23:59:59';
        $affected = RepayReliefRecordModel::create([
            'loan_id' => $data['caseCode'],
            'period' => $data['period'],
            'relief_amount' => $data['reliefAmount'] *100,
            'status' => RepayReliefRecordModel::STATUS_UNUSED,// 未使用
            'deadline' => $deadline,
        ]);
        if ($affected) {
            $returnData['result'] = CollectionApiRequest::RESPONSE_OK;
            $returnData['msg'] = '减免成功';
        } else {
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '数据库繁忙,请稍后重试';
        }
        return collectionResp($returnData);
    }

}
